package com.sgb.kjwl.view.ui.activity.self.goods;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ViewStorageChildBinding;
import com.sgb.kjwl.model.entity.business.StorageManEntity;
import com.sgb.kjwl.view.widget.StoragePopWindow;
import com.swgk.core.customview.ItemInputTextView;

import java.util.List;

/**
 * 仓储view及弹窗
 */
public class StorageChildView extends LinearLayout implements ItemInputTextView.OnWholeItemClickListener {
    private Context mContext;
    private ViewStorageChildBinding viewBinding;
    //仓储弹窗
    private StoragePopWindow storagePopWindow;
    private List<StorageManEntity> storageManEntityList;
    private String search;

    private ISelfView iSelfView;

    public interface ISelfView {
        void getData(String search);

        void setData(StorageManEntity storageEntity);
    }

    public void onSelfViewClick(ISelfView iSelfView) {
        this.iSelfView = iSelfView;
    }

    public StorageChildView(Context context) {
        this(context, null);
    }

    public StorageChildView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StorageChildView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public StorageChildView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    private void initView() {
        viewBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.view_storage_child, this, true);
        if (storagePopWindow == null) {
            storagePopWindow = new StoragePopWindow(mContext);
        }
        storagePopWindow.setBgView(viewBinding.grayLayout);
        storagePopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        storagePopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        storagePopWindow.setOnResultClick((key1, key2, key3) -> {
            StorageManEntity storageManEntity = (StorageManEntity) key1;
            if (storageManEntity == null) {
                return;
            }
            setLabText(storageManEntity.getStorageName());
            iSelfView.setData(storageManEntity);
        });
        storagePopWindow.inputTextInterface(s -> {
            search = s;
            iSelfView.getData(search);
        });
        viewBinding.itemTvStorage.setOnWholeItemClickListener(this);
    }

    @Override
    public void OnWholeItemClick(View view) {
        switch (view.getId()) {
            case R.id.item_tv_storage:
                if (storageManEntityList != null && storageManEntityList.size() != 0) {
                    storagePopWindow.showDownPopwindow(viewBinding.grayLayout, true);
                } else {
                    iSelfView.getData(search);
                }
                break;
        }
    }

    public void setLabText(String s) {
        viewBinding.itemTvStorage.setContent(s);
    }

    public String getLabText() {
        return viewBinding.itemTvStorage.getContent();
    }


    public void setStorageData(List<StorageManEntity> datas, boolean isUpdate) {
        this.storageManEntityList = datas;
        if (!storagePopWindow.isShowing()) {
            storagePopWindow.showDownPopwindow(viewBinding.grayLayout, true);
        }
        storagePopWindow.setData(storageManEntityList, isUpdate);
    }
}
